package com.sms.web.domain.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sms.common.persistence.DynamicSpecifications;
import com.sms.common.persistence.SearchFilter;
import com.sms.domain.entity.ChannelConfig;
import com.sms.domain.service.ChannelConfigService;
import com.sms.web.domain.dao.ChannelConfigDao;

/**
 * @desc 
 * @author wjw
 * @time 2017年11月16日下午3:33:42
 */
@Service
@Transactional
public class ChannelConfigServiceImpl implements ChannelConfigService {
    
    protected Logger logger = LoggerFactory.getLogger(getClass());
    
    @Autowired
    private ChannelConfigDao channelConfigDao;
    
    @Override
    @Transactional(readOnly = true)
    public ChannelConfig getOne(String id) {
        return channelConfigDao.getOne(id);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<ChannelConfig> findAll(int page, int pageSize) {
        Pageable pageable = new PageRequest(page, pageSize, new Sort(Direction.DESC, "channelConfigId"));
        return channelConfigDao.findAll(pageable);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<ChannelConfig> findAll(Map<String, Object> searchParams, int pageNumber, int pageSize) {
        
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "channelConfigId"));
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<ChannelConfig> spec = DynamicSpecifications.bySearchFilter(filters.values(), ChannelConfig.class);
        return channelConfigDao.findAll(spec, pageRequest);
        
    }
    
    /**
     * 查询所有有效的通道
     * @return xx
     */
    @Override
    @Transactional(readOnly = true)
    public List<ChannelConfig> findAll() {
        return channelConfigDao.findAll();
    }
    
    /**
     * 保存实体
     * @param ChannelConfig xx
     * @return xx
     */
    @Override
    public ChannelConfig save(ChannelConfig ChannelConfig) {
        return channelConfigDao.save(ChannelConfig);
    }
    
    /**
     * 物理删除
     * @param ChannelConfigId xx
     */
    @Override
    public void delete(String ChannelConfigId) {
        channelConfigDao.deleteById(ChannelConfigId);
    }
    
    /**
     * @param channelId x
     * @return x
     */
    @Override
    public int queryApplicationCount(String channelId) {
        return channelConfigDao.queryApplicationCount(channelId);
    }
}
